from utils.sql_utils import SQLManager
from utils.time_utils import TimeUtils
from utils.id_utils import IdUtils
from flask import request, jsonify
# from flask import request

class CommonInsertSerevice:
    def insert_item_by_arg(arg, request):

        base_utils = {
            'unique_id':IdUtils.generate_id(),
            'created_at':TimeUtils.get_current_time()
        }

        # 根据 arg 参数选择对应的 SQL 语句
        insertSqlMap = {
            "note": """
                INSERT INTO rb_note_content (
                    unique_id, user_id, slide_img_url,
                    title, content, image_url,
                    map_info_id, created_at) 
                VALUES ( %s, %s, %s, %s, %s, %s, %s, %s);
            """,
            "comment": """
                INSERT INTO rb_note_comments (
                    unique_id, note_id, user_id,
                    content, created_at)
                VALUES ( %s, %s, %s, %s, %s);
            """,
            "sub_comment": """
                INSERT INTO rb_note_sub_comments (
                    unique_id, comment_id, user_id,
                    content, created_at)
                VALUES ( %s, %s, %s, %s, %s);
            """,
            "note_likes": """
                INSERT INTO rb_note_likes (
                    unique_id, note_id, user_id,
                    created_at)
                VALUES ( %s, %s, %s, %s);
            """,
            "comment_likes": """
                INSERT INTO rb_note_comment_likes (
                    unique_id, comment_id, user_id,
                    created_at)
                VALUES ( %s, %s, %s, %s);
            """,
            "follows": """ 
                INSERT INTO rb_note_follows (
                    unique_id, user_id, followee_id,
                    created_at)
                VALUES ( %s, %s, %s, %s);
            """,
            "collections": """
                INSERT INTO rb_note_collections (
                    unique_id, user_id, note_id,
                    created_at)
                VALUES ( %s, %s, %s, %s);
            """,
            "slider_imgs": """
                INSERT INTO rb_note_slide_imgs (
                    unique_id, note_id, slide_img_url,
                    created_at)
                VALUES ( %s, %s, %s, %s);
            """,
            "map_info": """
                INSERT INTO rb_note_map_info (
                    unique_id, user_id, content,
                    location, longitude, latitude, created_at)
                VALUES ( %s, %s, %s, %s, %s, %s, %s);
            """
        }

        # 获取对应的 SQL 语句
        sql = insertSqlMap.get(arg)
        if not sql:
            raise ValueError(f"Invalid argument: {arg}")
        
        req = request.get_json()
        # 根据 arg 参数提取 items 中的值
        if arg == "note":
            map_info_id = IdUtils.generate_id()
            values = (
                base_utils['unique_id'], req.get('user_id'),
                req.get('slide_img_url'),req.get('title'),
                req.get('content'), req.get('image_url'),
                map_info_id, base_utils['created_at']
            )
            data = SQLManager().insert(sql, values)
            return {"data":data,"note_id":base_utils['unique_id'],'map_info_id':map_info_id}
        
        elif arg == "comment":
            values = (
                base_utils['unique_id'],req.get('note_id'), req.get('user_id'),
                req.get('content'), base_utils['created_at']
            )
        elif arg == "sub_comment":
            values = (
                base_utils['unique_id'], req.get('comment_id'), req.get('user_id'),
                req.get('content'), base_utils['created_at']
            )
        elif arg == "note_likes":
            values = (
                base_utils['unique_id'],req.get('note_id'), req.get('user_id'),
                base_utils['created_at']
            )
        elif arg == "comment_likes":
            values = (
                base_utils['unique_id'], req.get('comment_id'),req.get('user_id'),
                base_utils['created_at']
            )
        elif arg == "follows":
            values = (
                base_utils['unique_id'], req.get('user_id'), req.get('followee_id'),
                base_utils['created_at']
            )
        elif arg == "collections":
            values = (
                base_utils['unique_id'], req.get('user_id'), req.get('note_id'),
                base_utils['created_at']
            )
        elif arg == "slider_imgs":
            values = (
                base_utils['unique_id'], '',req.get('slide_img_url'),
                base_utils['created_at']
            )
        elif arg == "map_info":
            values = (
                req.get('map_info_id'), req.get('user_id'),req.get('content'),
                req.get('location'), req.get('longitude'), req.get('latitude'), base_utils['created_at']
            )
        # 执行 SQL 插入操作
        # 假设 self.execute_sql 是一个执行 SQL 的方法
        # self.execute_sql(sql, values)
        data = SQLManager().insert(sql, values)

        return {"data":data,"unique_id":base_utils['unique_id']}
